package com.example.module_orders.view

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.text.Editable
import android.text.TextWatcher
import android.widget.PopupMenu
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.module_orders.R
import com.example.module_orders.adapter.OrdersAdapter
import com.example.module_orders.vm.OrdersViewModel
import com.example.module_orders.databinding.ActivityOrdersBinding
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import com.yuwei.http.entity.VisitorEntityItem
import com.yuwei.http.entity.ordersEntity
import com.yuwei.http.entity.ordersEntityItem
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.ORDERS)
class OrdersActivity : BaseActivity<ActivityOrdersBinding, OrdersViewModel>() {
    val  ordersAdapter = OrdersAdapter()
    val  ordersData = mutableListOf<ordersEntityItem>()
    var isCheck = false
    var position = 0
    var menuTitle = ""
    @SuppressLint("NotifyDataSetChanged")
    override fun initView() {
        binding.ordersRv.isNestedScrollingEnabled =false
        binding.ordersRv.adapter = ordersAdapter
        binding.ordersRv.layoutManager = LinearLayoutManager(this)
        vm.getOrdersList()
        binding.ordersBack.setOnClickListener {
            finish()
        }
        binding.addOrders.setOnClickListener {
            ARouter.getInstance().build(RouterPath.add_ORDERS).navigation()
        }
        binding.ordersPx.setOnClickListener {
            isCheck = !isCheck
            if (isCheck){
                //正
                Glide.with(this).load(R.drawable.sx).into(binding.orderPxImage)
                ordersAdapter.data.sortBy { it.repairTime }
                ordersAdapter.notifyDataSetChanged()
            }else{
                //倒
                Glide.with(this).load(R.drawable.jx).into(binding.orderPxImage)
                ordersAdapter.data.sortByDescending { it.repairTime }
                ordersAdapter.notifyDataSetChanged()
            }
        }
        binding.orderSx.setOnClickListener {
            val popupMenu = PopupMenu(this,it)
            popupMenu.menuInflater.inflate(R.menu.order_type_menu,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener { item->
                when(item.itemId){
                    R.id.orders_all ->{
                        position = 0
                        menuTitle = "全部"
                        vm.getOrdersList()
                        ordersAdapter.notifyDataSetChanged()
                        true
                    }
                    R.id.orders_dcl ->{
                        position = 1
                        menuTitle = "待处理"
                        vm.getOrdersList()
                        ordersAdapter.notifyDataSetChanged()
                        true
                    }
                    R.id.orders_clz ->{
                        position = 2
                        menuTitle = "处理中"
                        vm.getOrdersList()
                        ordersAdapter.notifyDataSetChanged()
                        true
                    }
                    R.id.orders_ywc ->{
                        position = 3
                        menuTitle = "已完成"
                        vm.getOrdersList()
                        ordersAdapter.notifyDataSetChanged()
                        true
                    }
                    else -> onOptionsItemSelected(item)
                }
            }
            popupMenu.show()
        }
        ordersAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setTitle("删除提醒")
                .setMessage("确定要删除维修单吗？")
                .setPositiveButton("确定"){dialod,_ ->
                    vm.getDeleteOrders(ordersAdapter.data[position].id)
                    dialod.dismiss()
                }
                .setNegativeButton("取消"){item,_->
                    item.dismiss()
                }
                .create()
                .show()

            true
        }

        binding.ordersEd.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                ordersAdapter.data.clear()
                val text = s.toString().trim()
                val filterList = ordersData.filter {
                    it.orderNumber.contains(text, ignoreCase = true)

                }
                ordersAdapter.data.addAll(filterList)
                ordersAdapter.notifyDataSetChanged()
            }

        })
    }

    override fun initData() {
        vm.ordersSuccess.observe(this){
            ordersData.clear()
            ordersAdapter.data.clear()
            if (position!=0){
                ordersData.addAll(it.filter {item->
                    item.processingStatus == menuTitle
                })
                ordersAdapter.data.addAll(ordersData.reversed())
                ordersAdapter.notifyDataSetChanged()
            }else{
                ordersData.addAll(it)
                ordersAdapter.data.addAll(ordersData.reversed())
                ordersAdapter.notifyDataSetChanged()
            }
        }
        vm.deleteSuccess.observe(this){
            ToastUtils.showLong("删除成功")
            vm.getOrdersList()
            ordersAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getOrdersList()
    }


}
